﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace BLL
{
	public class QLNhatKy
	{
		static QLNhatKy()
		{

		}
		public static IEnumerable<NhatKySuDung> GetAll()
		{
            NhatKySuDung q = new NhatKySuDung();
			return Generic.GetAll<NhatKySuDung>();
		}
        public static ActionResult ThemNhatKy(string ten_congviec )
        {
            NhatKySuDung nksd = new NhatKySuDung();
            nksd.MaNguoiDung = MyAccount.account.MaTK;
            nksd.TenCongViec = ten_congviec;
            nksd.ThoiGianHoatDong = DateTime.Now;
            return Insert(nksd);
        }
        public static ActionResult Insert(NhatKySuDung mt)
        {
            ActionResult kq = new ActionResult();
            try
            {
                 Generic.Insert<NhatKySuDung>(mt);
            }
            catch(Exception ex)
            {
                kq.Exceptions.Add(ex);
            }
            return kq;
        }
		public static int Delete(NhatKySuDung mt)
		{
			return Generic.DeleteByCondition<NhatKySuDung>(mt1 => mt1.MaNK == mt.MaNK);
		}
		public static int DeleteAll(IEnumerable<NhatKySuDung> ps)
		{
			return Generic.DeleteByCondition<NhatKySuDung>(mt => ps.Where(x => x.MaNK == mt.MaNK).Count() == 1);
		}
		public static int DeleteByMaNK(int maNK)
		{
			return Generic.DeleteByCondition<NhatKySuDung>(nk1 => nk1.MaNK == maNK);
		}
		public static bool DeleteByID(int mank)
		{
			return Generic.DeleteByCondition<NhatKySuDung>(x => x.MaNK == mank) > 0;

		}
		/// <summary>
		/// Find 1 thiết bị theo Điều Kiện.
		/// </summary>
		public static IEnumerable<NhatKySuDung> FindByCondition(Func<NhatKySuDung, bool> func)
		{
			return Generic.FindByCondition<NhatKySuDung>(func);
		}
		/// <summary>
		/// Tim Kiếm Theo Dieu Kien
		/// </summary>
		public enum DKTimKiem
		{
			MaNguoiDung = 0,
			ThoiGianHoatDong,
			NoiDungCongViec,
			MaNhatKi
		};
		public static void TimKiem(DataGridView dtgvQLNK, ComboBox cbbLoaiTimkiem, DKTimKiem dk, string dktimkiem)
		{
			try
			{
				switch (dk)
				{
					case DKTimKiem.MaNguoiDung:
						dtgvQLNK.DataSource = QLNhatKy.FindByCondition(x => Regex.IsMatch(x.MaNguoiDung.ToLower(), dktimkiem.ToLower()));
						break;
					case DKTimKiem.ThoiGianHoatDong:
						dtgvQLNK.DataSource = QLNhatKy.FindByCondition(x => x.ThoiGianHoatDong != null && Regex.IsMatch(x.ThoiGianHoatDong.ToString().ToLower(), dktimkiem.ToLower()));
						break;
					case DKTimKiem.MaNhatKi:
						dtgvQLNK.DataSource = QLNhatKy.FindByCondition(x => Regex.IsMatch(x.MaNK.ToString().ToLower(), dktimkiem.ToLower()));
						break;
					default:
						break;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
